package a07_二叉树;

/**
 * <p>
 * a24_二叉搜索树的最小绝对差
 * </p>
 *
 *
 * @author flyduck
 * @since 2024/12/19
 */
public class a24_二叉搜索树的最小绝对差 {

    public static void main(String[] args) {
        //236,104,701,null,227,null,911
        TreeNode treeNode = new TreeNode(236,
                new TreeNode(104,
                        null,
                        new TreeNode(227)),
                new TreeNode(701,
                        null,
                        new TreeNode(911)));
        int minimumDifference = getMinimumDifference(treeNode);
        System.out.println(minimumDifference);
    }

    public static int getMinimumDifference(TreeNode root) {
        traversal(root);
        return minimumDifference;
    }

    private static int minimumDifference = Integer.MAX_VALUE;//最小的差
    private static TreeNode prevNode = null;

    public static void traversal(TreeNode currentNode){
        if (currentNode == null) {
            return;
        }

        traversal(currentNode.left);

        if (prevNode != null) {//说明不是第一个遍历节点了
            minimumDifference = Math.min(currentNode.val - prevNode.val, minimumDifference);
        }
        prevNode = currentNode;

        traversal(currentNode.right);
    }
}
